#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ZO.SmartCore.Windows.Forms.Design;

#endregion

namespace ZO.SmartCore.Windows.Forms
{
	/// <summary>
	/// Represent a control to display title
	/// </summary>
	[ToolboxBitmap(typeof(Label))]
	[ToolboxItem(true)]
    [Designer(typeof(SmartTitleDesigner))]
	public class SmartTitle : SmartControl
	{

		#region Constructors
		/// <summary>
        /// Initializes a new instance of the <see cref="SmartTitle"/> class.
		/// </summary>
		public SmartTitle()
		{
			InitializeComponent();

			// Avoid receiving the focus.
			SetStyle(ControlStyles.Selectable, false);

		}
		#endregion

		#region Destructor

		#endregion

		#region Fields
		private Color _Color1 = SystemColors.GradientInactiveCaption;
		private Color _Color2 = SystemColors.InactiveCaptionText;

		private Orientation _Orientation = Orientation.Horizontal;

		private ContentAlignment _TextAlign = ContentAlignment.MiddleCenter;

		private Padding _TextGap = new Padding(2);
		#endregion

		#region Events

		#endregion

		#region Operators

		#endregion

		#region Properties


		/// <summary>
		/// Gets or sets the text gap.
		/// </summary>
		/// <value>The text gap.</value>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public Padding TextGap
		{
			get
			{
				return this._TextGap;
			}
			set
			{
				this._TextGap = value;
				this.Invalidate();
			}
		}


		/// <summary>
		/// Gets or sets the dark color of the control.
		/// </summary>
		/// <value>dark color of the control.</value>
		[Category("Gradient")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public Color Color1
		{

			get { return _Color1; }

			set
			{
				_Color1 = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the light color of the control.
		/// </summary>
		/// <value>light color of the control.</value>
		[Category("Gradient")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public Color Color2
		{
			get { return _Color2; }

			set
			{
				_Color2 = value;
				this.Invalidate();
			}
		}

		/// <summary>
        /// Gets or sets a value indicating whether this <see cref="SmartTitle"/> is transparent.
		/// </summary>
		/// <value><c>true</c> if transparent; otherwise, <c>false</c>.</value>
		[Browsable(false)]
		public override bool Transparent
		{
			get { return base.Transparent; }
			set
			{
			}
		}



		/// <summary>
		/// Gets or sets the orientation.
		/// </summary>
		/// <value>The orientation.</value>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.All)]
		public Orientation Orientation
		{
			get
			{
				return _Orientation;
			}
			set
			{
				if (this.Orientation != value)
				{
					_Orientation = value;
					Refresh();
				}
			}
		}

		/// <summary>
		/// Gets or sets the alignment of text. 
		/// </summary>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.All)]
		public virtual ContentAlignment TextAlign
		{
			get
			{
				return this._TextAlign;
			}
			set
			{
				this._TextAlign = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the alignment of text. 
		/// </summary>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public override Color ForeColor
		{
			get
			{
				return base.ForeColor;
			}
			set
			{
				base.ForeColor = value;
				this.Invalidate();

			}
		}

		/// <summary>
		/// Gets or sets the text associated with this control.
		/// </summary>
		/// <value></value>
		/// <returns>The text associated with this control.</returns>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		[Browsable(true)]
		public override string Text
		{
			get
			{
				return base.Text;
			}
			set
			{
				base.Text = value;
				this.Invalidate();
			}
		}
		#endregion

		#region Methods
		/// <summary>
		/// Raises the <see cref="Control.Resize"/> event.
		/// </summary>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected override void OnResize(EventArgs e)
		{
			this.Invalidate();
			base.OnResize(e);
		}

		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.Control.Paint"></see> event.
		/// </summary>
		/// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that contains the event data.</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics g = e.Graphics;

			g.SmoothingMode = SmoothingMode.HighQuality;

			Rectangle rect = this.ClientRectangle;

			LinearGradientMode mode;

			if (this.Orientation == Orientation.Horizontal)
			{
				mode = LinearGradientMode.Horizontal;
			}
			else
			{
				mode = LinearGradientMode.Vertical;
			}


			LinearGradientBrush lb = new LinearGradientBrush(rect, this.Color1,
				this.Color2, mode);

			g.FillRectangle(lb, rect);

			// draw text on label
			SolidBrush drawBrush = new SolidBrush(this.ForeColor);
			StringFormat sf = new StringFormat();

			// align text
			switch (this.TextAlign)
			{
				case ContentAlignment.BottomCenter:
					sf.Alignment = StringAlignment.Center;
					sf.LineAlignment = StringAlignment.Far;
					break;

				case ContentAlignment.BottomLeft:
					sf.Alignment = StringAlignment.Near;
					sf.LineAlignment = StringAlignment.Far;
					break;
	
				case ContentAlignment.BottomRight:
					sf.Alignment = StringAlignment.Far;
					sf.LineAlignment = StringAlignment.Far;
					break;

				case ContentAlignment.MiddleCenter:
					sf.Alignment = StringAlignment.Center;
					sf.LineAlignment = StringAlignment.Center;
					break;

				case ContentAlignment.MiddleLeft:
					sf.Alignment = StringAlignment.Near;
					sf.LineAlignment = StringAlignment.Center;
					break;

				case ContentAlignment.MiddleRight:
					sf.Alignment = StringAlignment.Far;
					sf.LineAlignment = StringAlignment.Center;
					break;

				case ContentAlignment.TopCenter:
					sf.Alignment = StringAlignment.Center;
					sf.LineAlignment = StringAlignment.Near;
					break;

				case ContentAlignment.TopLeft:
					sf.Alignment = StringAlignment.Near;
					sf.LineAlignment = StringAlignment.Near;
					break;

				case ContentAlignment.TopRight:
					sf.Alignment = StringAlignment.Far;
					sf.LineAlignment = StringAlignment.Near;
					break;
			}
			// set rectangle bound text
			// output string

			RectangleF rect2 = new RectangleF();

			rect2.Size = new SizeF(this.Width - this.TextGap.Left - this.TextGap.Right, this.Height - this.TextGap.Top - this.TextGap.Bottom);
			rect2.Location = new PointF(this.TextGap.Left, this.TextGap.Top);

			g.DrawString(this.Text, this.Font, drawBrush, rect2, sf);

			base.OnPaint(e);
		}


		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// Line
			// 
			this.Name = "SmartTitle";
			this.Size = new Size(50, 50);
			this.ForeColor = SystemColors.InactiveCaptionText;
			this.ResumeLayout(false);

		}

		#endregion
	}
}
